home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3 / CHAPTER3 / ENUMTOOL.C < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  285 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "enumtool.h"  
  5.  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "TTM_ENUMTOOLS"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static MSG      msg;
  111. static TOOLINFO ti;
  112. static int      index;
  113.  
  114. static HWND hToolTip = NULL;
  115.  
  116.    switch( uMsg )
  117.    {
  118.       case WM_LBUTTONDOWN :
  119.       case WM_MOUSEMOVE   :
  120.       case WM_LBUTTONUP   :
  121.       case WM_RBUTTONDOWN :
  122.       case WM_MBUTTONDOWN :
  123.       case WM_RBUTTONUP   :
  124.       case WM_MBUTTONUP   :
  125.               msg.hwnd    = hWnd;
  126.               msg.message = uMsg; 
  127.               msg.wParam  = wParam;
  128.               msg.lParam  = lParam;
  129.               msg.time    = GetMessageTime();
  130.               msg.pt.x    = LOWORD(GetMessagePos());
  131.               msg.pt.y    = HIWORD(GetMessagePos());
  132.  
  133.               SendMessage( hToolTip, TTM_RELAYEVENT, 0, (LPARAM)&msg );
  134.               break;
  135.    }
  136.  
  137.    switch( uMsg )
  138.    {
  139.       case WM_CREATE :
  140.               {
  141.                  InitCommonControls();
  142.  
  143.                  // Create tool tip control.
  144.                  //.........................
  145.                  hToolTip = CreateWindowEx( 0,
  146.                                             TOOLTIPS_CLASS, NULL, 
  147.                                             TTS_ALWAYSTIP,
  148.                                             CW_USEDEFAULT, 
  149.                                             CW_USEDEFAULT, 
  150.                                             CW_USEDEFAULT, 
  151.                                             CW_USEDEFAULT,
  152.                                             hWnd, NULL, 
  153.                                             hInst, NULL );
  154.  
  155.                  // Add the tools to the tool tip control.
  156.                  //.......................................
  157.                  ti.cbSize = sizeof( TOOLINFO );
  158.                  ti.hwnd   = hWnd;
  159.                  ti.hinst  = NULL;
  160.  
  161.                  for( index=0; index < 4; index++ )
  162.                  {
  163.                     ti.uId      = index;
  164.                     ti.lpszText = LPSTR_TEXTCALLBACK;
  165.  
  166.                     ti.rect.top    = 0;
  167.                     ti.rect.bottom = 10;
  168.                     ti.rect.left   = 10*index;
  169.                     ti.rect.right  = 10*(index+1);
  170.  
  171.                     SendMessage( hToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti );
  172.                  }
  173.               }
  174.               break;
  175.  
  176.       case WM_SIZE :
  177.               {
  178.                  // Adjust the tool rectangles for the new
  179.                  // section size.
  180.                  //.......................................
  181.                  RECT     rect;
  182.  
  183.                  index = 0;
  184.  
  185.                  ti.cbSize = sizeof( TOOLINFO );
  186.  
  187.                  GetClientRect( hWnd, &rect );
  188.  
  189.                  while ( SendMessage( hToolTip, TTM_ENUMTOOLS, 
  190.                          index, (LPARAM)&ti ) )
  191.                  {
  192.                     ti.rect.top    = 0;
  193.                     ti.rect.bottom = HIWORD( lParam );
  194.                     ti.rect.left   = (rect.right/4)*index;
  195.                     ti.rect.right  = (rect.right/4)*(index+1);
  196.  
  197.                     SendMessage( hToolTip, TTM_SETTOOLINFO, index, (LPARAM)&ti );
  198.  
  199.                     index++;
  200.                  }
  201.               }
  202.               break;
  203.  
  204.       case WM_PAINT  :
  205.               {
  206.                  // Paint the vertical lines to denote the sections.
  207.                  //.................................................
  208.                  PAINTSTRUCT ps;
  209.  
  210.                  index = 0;
  211.  
  212.                  BeginPaint( hWnd, &ps );
  213.  
  214.                  while ( SendMessage( hToolTip, TTM_ENUMTOOLS, 
  215.                          index, (LPARAM)&ti ) )
  216.                  {
  217.                     MoveToEx( ps.hdc, ti.rect.right, ti.rect.top, NULL );
  218.                     LineTo( ps.hdc, ti.rect.right, ti.rect.bottom );
  219.                     index++;
  220.                  }
  221.  
  222.                  EndPaint( hWnd, &ps );
  223.               }
  224.               break;
  225.  
  226.       case WM_NOTIFY :
  227.               switch ( ((LPNMHDR)lParam)->code )
  228.               {
  229.                  case TTN_NEEDTEXT :
  230.                       wsprintf( ((LPTOOLTIPTEXT)lParam)->szText, 
  231.                                 "Section %d", 
  232.                                 ((LPTOOLTIPTEXT)lParam)->hdr.idFrom+1 );
  233.                       break;
  234.               }
  235.               break;
  236.  
  237.  
  238.       case WM_COMMAND :
  239.               switch( LOWORD( wParam ) )
  240.               {
  241.                  case IDM_ABOUT :
  242.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  243.                         break;
  244.  
  245.                  case IDM_EXIT :
  246.                         DestroyWindow( hWnd );
  247.                         break;
  248.               }
  249.               break;
  250.       
  251.       case WM_DESTROY :
  252.               PostQuitMessage(0);
  253.               break;
  254.  
  255.       default :
  256.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  257.    }
  258.  
  259.    return( 0L );
  260. }
  261.  
  262.  
  263. LRESULT CALLBACK About( HWND hDlg,           
  264.                         UINT message,        
  265.                         WPARAM wParam,       
  266.                         LPARAM lParam)
  267. {
  268.    switch (message) 
  269.    {
  270.        case WM_INITDIALOG: 
  271.                return (TRUE);
  272.  
  273.        case WM_COMMAND:                              
  274.                if (   LOWORD(wParam) == IDOK         
  275.                    || LOWORD(wParam) == IDCANCEL)    
  276.                {
  277.                        EndDialog(hDlg, TRUE);        
  278.                        return (TRUE);
  279.                }
  280.                break;
  281.    }
  282.  
  283.    return (FALSE); 
  284. }
  285.